﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.BusinessEntity.YunZhiJia;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 保存自动获取计划跟踪号
    /// </summary>
    /// <remarks>
    /// 功能：保存自动获取计划跟踪号
    /// 创建人：mey
    /// 创建日期：2024.4.08
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    [Description("保存自动获取计划跟踪号")]
    [HotUpdate]
    public class SalOrderSaveServer : AbstractOperationServicePlugIn
    {
        //OnPreparePropertys 数据加载前，确保需要的属性被加载
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);

            //单据编码
            e.FieldKeys.Add("FBillNo");
            //生效日期
            e.FieldKeys.Add("F_DEV_Offset");
            //库存组织
            e.FieldKeys.Add("FMaterialId");
            //库存组织
            e.FieldKeys.Add("fseq");
            //计划跟踪号
            e.FieldKeys.Add("FMtoNo");
            
        }
        /// <summary>
        /// 事务结束后事件
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);

            //读取全部的单据,for循环,转换成DynamicObject类型
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string FID = entity["ID"].ToString();
                string FBillNo = entity["BillNo"].ToString();
                string Offset = entity["F_DEV_Offset"].ToString();//定制
                if (Offset == "1")
                {
                    DynamicObjectCollection FEntityDetail = entity["SaleOrderEntry"] as DynamicObjectCollection;//明细 


                    foreach (var item in FEntityDetail)
                    {
                        string FENTRTYID = Convert.ToString(item["id"]);
                        string MaterialId = "";//物料内码
                        string seq = Convert.ToString(item["seq"]);
                        string FMTONO= Convert.ToString(item["MtoNo"]);
                        DynamicObject Material = item["MaterialId"] as DynamicObject;//物料
                        if (Material != null && (FMTONO == "" || FMTONO == " "))
                        {
                            MaterialId = Convert.ToString(Material["Id"]);
                            string SQL = @"/*dialect*/select b.FMfgPolicyId from  T_BD_MATERIAL a
                            inner join t_BD_MaterialPlan b on a.FMaterialId=b.FMaterialId  where a.FMaterialId='" + MaterialId + "'";
                            DataTable dt = DBServiceHelper.ExecuteDataSet(this.Context, SQL).Tables[0];
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                if (dt.Rows[0]["FMfgPolicyId"].ToString() == "40044")
                                {
                                    string mtono = FBillNo + "-" + seq;

                                    string SU_S = @"/*dialect*/update T_SAL_ORDERENTRY set FMtoNo='" + mtono + "' where fentryid='" + FENTRTYID + "'";
                                    DBServiceHelper.Execute(this.Context, SU_S);

                                }
                                else
                                {
                                    string SU_S = @"/*dialect*/update T_SAL_ORDERENTRY set FMtoNo='' where fentryid='" + FENTRTYID + "'";
                                    DBServiceHelper.Execute(this.Context, SU_S);

                                }
                            }
                        }
                    }

                }
                else
                {
                    DynamicObjectCollection FEntityDetail = entity["SaleOrderEntry"] as DynamicObjectCollection;//明细 


                    foreach (var item in FEntityDetail)
                    {
                        string FENTRTYID = Convert.ToString(item["id"]);



                        string SU_S = @"/*dialect*/update T_SAL_ORDERENTRY set FMtoNo='' where fentryid='" + FENTRTYID + "'";
                        DBServiceHelper.Execute(this.Context, SU_S);


                    }
                }
            }
        }



    }
}
